package com.puyuba.puyuba.utils.custommessage;

import android.os.Bundle;
import android.view.View;

import com.hyphenate.EMMessageListener;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMUserInfo;
import com.hyphenate.easeui.EaseIM;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.manager.EaseSystemMsgManager;
import com.hyphenate.easeui.modules.conversation.EaseConversationListFragment;
import com.hyphenate.easeui.modules.conversation.model.EaseConversationInfo;
import com.hyphenate.easeui.provider.EaseUserProfileProvider;
import com.puyuba.puyuba.R;
import com.puyuba.puyuba.SkinApplication;
import com.puyuba.puyuba.view.activity.ChatActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 消息列表页面
 */
public class ConversationListFragment extends EaseConversationListFragment {

    private EMMessageListener msgListener;
    private static String SHARED_KEY_CURRENTUSER_NICK = "SHARED_KEY_CURRENTUSER_NICK";

    //跳转聊天页面
    @Override
    public void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        Object info = conversationListLayout.getItem(position).getInfo();
        if(info instanceof EMConversation) {
            if(EaseSystemMsgManager.getInstance().isSystemConversation((EMConversation) info)) {

            }else {
                ChatActivity.startActivity(mContext,((EMConversation)info).conversationId());
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        //返回之后刷新list
        conversationListLayout.refreshList();
        conversationListLayout.loadDefaultData();
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        conversationListLayout.setBackgroundColor(getContext().getColor(R.color.white));
//        conversationListLayout.showSystemMessage(true);

        //注册消息监听接受消息
        msgListener = new EMMessageListener() {
            @Override
            public void onMessageReceived(List<EMMessage> messages) {
                conversationListLayout.refreshList();
                conversationListLayout.loadDefaultData();
            }

            @Override
            public void onCmdMessageReceived(List<EMMessage> messages) {
                conversationListLayout.refreshList();
            }

            @Override
            public void onMessageRead(List<EMMessage> messages) {

            }

            @Override
            public void onMessageDelivered(List<EMMessage> messages) {

            }

            @Override
            public void onMessageRecalled(List<EMMessage> messages) {

            }

            @Override
            public void onMessageChanged(EMMessage message, Object change) {

            }
        };
        EMClient.getInstance().chatManager().addMessageListener(msgListener);

        Map<String, EMConversation> allConversations = EMClient.getInstance()
                .chatManager().getAllConversations();
        List<String> list = new ArrayList<>();
        for (Map.Entry<String, EMConversation> entry : allConversations.entrySet()) {
            list.add(entry.getKey());
        }
        String[] users = list.toArray(new String[list.size()]);

        EMClient.getInstance().userInfoManager().fetchUserInfoByUserId(users,
                new EMValueCallBack<Map<String, EMUserInfo>>() {
                    @Override
                    public void onSuccess(Map<String, EMUserInfo> userInfos) {
                        for (String user : users) {
                            EMUserInfo emUserInfo = userInfos.get(user);
                            if (emUserInfo != null) {
                                SkinApplication.getInstance().addEaseUserInfo(user, emUserInfo);
                            }
                        }
                    }

                    @Override
                    public void onError(int error, String errorMsg) {

                    }
                });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        记得在不需要的时候移除listener，如在activity的onDestroy()时
        EMClient.getInstance().chatManager().removeMessageListener(msgListener);
    }

}
